package com.superman.supermarket.client.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.Registry;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.mescroll.UpAndDownCallBack;
import com.superman.supermarket.client.constant.GoodsConstant;
import com.superman.supermarket.client.event.CartPageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.cart.CartView;

/**
 * 购物车
 * 
 * @author superman
 * @version 2018年8月2日下午5:06:14
 */
public class CartActivity extends ShopAbstractActivity implements UpAndDownCallBack {

	@Inject
	CartView cartView;

	private List<Map<String, Object>> invalidSkusList = new ArrayList<Map<String, Object>>();

	private void deleteSku(String skuId) {
		List<String> skus = new ArrayList<String>();
		skus.add(skuId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("SKU_LIST", skus);
		postman.post("cart/delete", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				cartView.getPullToRefreshView().clearListAndResetScroll();
			}
		});
	}

	private List<Map<String, Object>> filterInvalidSkus(List<Map<String, Object>> list) {
		Iterator<Map<String, Object>> storeIt = list.iterator();
		while (storeIt.hasNext()) {
			Map<String, Object> storeMap = storeIt.next();
			List<Map<String, Object>> storeSkuList = XJsonUtil.getList((JSONArray) storeMap.get("SKU_LIST"));
			Iterator<Map<String, Object>> skuIt = storeSkuList.iterator();
			while (skuIt.hasNext()) {
				Map<String, Object> skuMap = skuIt.next();
				int state = (int) Double.parseDouble(String.valueOf(skuMap.get("STATE")));
				if (state != GoodsConstant.GOODS_STATE_ONSALE) {
					invalidSkusList.add(skuMap);
					skuIt.remove();
				}
			}

			if (storeSkuList.size() == 0) {
				storeIt.remove();
			}
		}
		return list;
	}

	@Override
	public void getListDataByPage(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PAGE_INDEX", -1);
		postman.post("cart/list", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				// 过滤失效的商品
				invalidSkusList.clear();
				cartView.resetBottom();
				cartView.clearDataList();
				if (XJsonUtil.getCount(response) == 0) {
					cartView.getPullToRefreshView().endBySize(null, 0);
					cartView.setBottomVisible(false);
				} else {
					List<Map<String, Object>> list = filterInvalidSkus(XJsonUtil.getList(response));
					cartView.setBottomVisible(list.size() > 0);
					cartView.getPullToRefreshView().endBySize(list, list.size());
					cartView.setBottomVisible(true);
					cartView.addInvalidSkusList(invalidSkusList);
				}
			}
		});
	}

	@Override
	public IPageView getView() {
		return cartView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(CartPageEvent.TYPE, new CartPageEvent.CartPageEventHandler() {

			@Override
			public void checkAll(boolean check) {
			}

			@Override
			public void checkSku(Map<String, Object> map) {
				// TODO Auto-generated method stub

			}

			@Override
			public void checkStore(String storeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void delete(String skuId) {
				deleteSku(skuId);
			}

			@Override
			public void settlement() {
				CartActivity.this.settlement();
			}

			@Override
			public void unCheckSku(Map<String, Object> map) {
				// TODO Auto-generated method stub

			}

			@Override
			public void uncheckStore(String storeId) {
				// TODO Auto-generated method stub

			}

			@Override
			public void updatePrice(Map<String, Object> map) {
			}

		});
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		cartView.getPullToRefreshView().initMeScroll(this, "1.2rem", "1.333333rem");
	}

	private void settlement() {
		final Map<String, Object> map = new HashMap<String, Object>();
		map.put("STORE_SKU_LIST", cartView.getCheckedStoreListData());
		postman.post("order/settlement2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				Registry.register("settlementinfo", XJsonUtil.getBody(response));
				Registry.register("preorderinfo", map);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("CLEAN_CART", 1);
				placeController.push(placeFactory.getPreorderPlace(), map);
			}
		});
	}

}
